package edu.tyut.game.logic

import edu.tyut.game.logic.Repository.fire
import edu.tyut.game.logic.model.Comment
import edu.tyut.game.logic.network.GameNetWork
import edu.tyut.game.utils.LogUtils
import okhttp3.MultipartBody

object FriendCircleRepository {

    private val tag = this::class.java.name

    fun postFriendCircle(parts: List<MultipartBody.Part>) = fire {
        LogUtils.d(tag, "发布朋友圈...")
        val res = GameNetWork.postFriendCircle(parts)
        if (res.status == "ok") {
            LogUtils.d(tag, res.msg)
            Result.success(res.data)
        } else {
            LogUtils.d(tag, res.msg)
            Result.failure(Exception(res.msg))
        }
    }

    fun deleteFriendCircle(fcId: Int) = fire {
        val res = GameNetWork.deleteFriendCircle(fcId)
        if (res.status == "ok") {
            LogUtils.d(tag, res.msg)
            Result.success(res.data)
        } else {
            LogUtils.d(tag, res.msg)
            Result.failure(Exception(res.msg))
        }
    }

    fun updateFriendCircle(parts: List<MultipartBody.Part>) = fire {
        val res = GameNetWork.updateFriendCircle(parts)
        LogUtils.d(tag, "更新朋友圈")
        if (res.status == "ok") {
            LogUtils.d(tag, res.msg)
            Result.success(res.data)
        } else {
            LogUtils.d(tag, res.msg)
            Result.failure(Exception(res.msg))
        }
    }

    fun queryFriendCircle(order: String) = fire {
        val res = GameNetWork.queryFriendCircle(order)
        if (res.status == "ok") {
            LogUtils.d(tag, res.msg)
            Result.success(res.data)
        } else {
            LogUtils.d(tag, res.msg)
            Result.failure(Exception(res.msg))
        }
    }

    fun addFriendCircleLike(fcId: Int) = fire {
        val res = GameNetWork.addFriendCircleLike(fcId)
        if (res.status == "ok") {
            LogUtils.d(tag, res.msg)
            Result.success(res.data)
        } else {
            LogUtils.d(tag, res.msg)
            Result.failure(Exception(res.msg))
        }
    }

    fun subFriendCircleLike(fcId: Int) = fire {
        val res = GameNetWork.subFriendCircleLike(fcId)
        if (res.status == "ok") {
            LogUtils.d(tag, res.msg)
            Result.success(res.data)
        } else {
            LogUtils.d(tag, res.msg)
            Result.failure(Exception(res.msg))
        }
    }

    fun postComment(comment: Comment) = fire {
        val res = GameNetWork.postComment(comment)
        if (res.status == "ok") {
            LogUtils.d(tag, res.msg)
            Result.success(res.data)
        } else {
            LogUtils.d(tag, res.msg)
            Result.failure(Exception(res.msg))
        }
    }

    fun deleteComment(id: Int) = fire {
        val res = GameNetWork.deleteComment(id)
        if (res.status == "ok") {
            LogUtils.d(tag, res.msg)
            Result.success(res.data)
        } else {
            LogUtils.d(tag, res.msg)
            Result.failure(Exception(res.msg))
        }
    }
    fun queryComment(fcId: Int) = fire {
        val res = GameNetWork.queryComment(fcId)
        if (res.status == "ok") {
            LogUtils.d(tag, "评论获取: ${res.data}")
            LogUtils.d(tag, res.msg)
            Result.success(res.data)
        } else {
            LogUtils.d(tag, res.msg)
            Result.failure(Exception(res.msg))
        }
    }
    fun addCommentLike(commentId: Int) = fire {
        val res = GameNetWork.addCommentLike(commentId)
        if (res.status == "ok") {
            LogUtils.d(tag, res.msg)
            Result.success(res.data)
        } else {
            LogUtils.d(tag, res.msg)
            Result.failure(Exception(res.msg))
        }
    }
    fun subCommentLike(commentId: Int) = fire {
        val res = GameNetWork.subCommentLike(commentId)
        if (res.status == "ok") {
            LogUtils.d(tag, res.msg)
            Result.success(res.data)
        } else {
            LogUtils.d(tag, res.msg)
            Result.failure(Exception(res.msg))
        }
    }
}